// Image functions
//

#ifndef __IMAGE_H__
#define __IMAGE_H__

#ifdef __cplusplus
extern "C" {
#endif

#include "Canvas.h"

#define __IMAGE_LIST_CACHE__	0		// enable this functiono not?

/////////////////////////////////////////////////////////////////////////////
// Image class

typedef struct 
{
/* read only */
	UInt16	width;		// width in pixels
	UInt16	height;		// height in pixels
	char	data[1];	// the raw data of pixels in scanline order
}	TImage;

#define IMAGE_WIDTH(im)			((im).width)
#define IMAGE_HEIGHT(im)		((im).height)
#define PIMAGE_WIDTH(pim)		((pim)->width)
#define PIMAGE_HEIGHT(pim)		((pim)->height)

#if __IMAGE_LIST_CACHE__
// ** add by Charlie 20060907 **************************************************
//#define MIN_IMAGE_CACHE_PAGE_NUM	5  			// minimum page number of cache
//#define MAX_IMAGE_CACHE_MEM_SIZE	1024*100	// depend on how many memory space we want to use

#define MAX_IMAGE_LOAD_SIZE			40000		// never load image large this size
												// if happens to use this image, it will use [x] to indicate it -OB

typedef struct PageInfo // contains information about image item that stored in this page
{
	Int16	nSerial; 	// serial number of image item, -1 if not used
	UInt16	nHit;		// count the using times of this data item, data with least value will be dropped.
} TPageInfo;

typedef struct Cache
{
	unsigned int	unPageNum;	// Max page number in cache
	unsigned int	unPageSize;	// page size
	unsigned int	unPageUsed;	// current number of pages contain data item
	TPageInfo		*pPageInfo;	// info of each page
	char			*pPage;  	// base address of cache memory
} TCache;

typedef struct ImageItemList
{
	unsigned short	Count;		// number of image item contained in file
	char			*filename;	// filename of file contains data items
	unsigned long	*Offsets;	// contains offset of each image item in file
	TCache			*pCache;	// points to the cache prepared for file
} TImageList;		// all images in files are called "image item"
					// they are numbered (from 0) by the sequence they stored in the file
					// the number is called "serial" in following code
// ** end by Charlie 20060907 **************************************************
#else
typedef struct 
{
/* private! plz don't access it */
    UInt16  Count;
//  TImage* pList;
    char*   pBuff;
}   TImageList;
#endif

int g_BillLoadImage(char* tImagebuffer, const char *filename);

// Load a image from file
// NOTE: when no longer need it, call the g_DeleteImage to delete it.
TImage* g_LoadImage(const char *filename);

// Create a image from memory
// NOTE: when no longer need it, call the g_DeleteImage to delete it.
TImage* g_CreateImage(int width, int height);

// Delete a image from memory
int  g_DeleteImage(TImage *image);

// Load list of images from file
// NOTE: when no longer need it, call the g_DeleteImageList to delete it.
TImageList* g_LoadImageList(const char *filename);

// Delete a list of images from memory
int  g_DeleteImageList(TImageList *image_list);

// Get a image from list
TImage* g_GetImageFromList(const TImageList *image_list, int serial);

// Draw images
void g_DrawMultiImages(const TCanvas *canvas, TPoint *points, int count, const TImageList *image_list, int *serials);

// Adjust the color space of image
void g_AdjustImage565to555(TImage *image);

void g_AdjustImage888BGRtoGBR(TImage *image);

#ifdef __cplusplus
}
#endif

#endif	// __IMAGE_H__
